package cn.com.headfree.mouldshow.dao.impl;

import cn.com.headfree.entity.TEmpBaseInfo;
import cn.com.headfree.mouldshow.dao.TEmpBaseInfoDao;
import cn.com.headfree.vo.UserDetailVo;
import cn.com.headfree.vo.UserVo;
import cn.com.headfree.vo.simple.EmpBaseInfoVo;
import com.gta.edu.sdk.base.dao.impl.BaseDaoImpl;
import com.gta.edu.sdk.util.StringUtil;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;

@Repository("/tEmpBaseInfoDao")
public class TEmpBaseInfoDaoImpl extends BaseDaoImpl<TEmpBaseInfo> implements TEmpBaseInfoDao {


    @Override
    public List<TEmpBaseInfo> findByErpNo(String erpNo) {
        Object[] params=new Object[1];
        StringBuilder sql=new StringBuilder("select * from t_emp_base_info g where=?");
        // g.erp_no='11238555'
        params[0]=erpNo;
        return this.findAllBySQL(TEmpBaseInfo.class,sql.toString(),null,false,params);
    }

    @Override
    public List<EmpBaseInfoVo> findByBaseorgId(String deptId) throws Exception{
        Object[] params=new Object[1];
//        StringBuilder sql=new StringBuilder("select t.id,u.`NAME` as user_name,t.erp_no,GET_DIC('Gender',t.sex) as sex,DATE_FORMAT(t.birth_date,'%Y-%m-%d %H:%i:%s') as birth_date_str,t.birth_date,GET_DIC('Nation',t.nation) as nation,t.birth_place,t.born_place,GET_DIC('PoliticalLandscapeText',t.politi_status) as politi_status,t.id_card_no,t.mobile_phone,GET_DIC('StaffLevel',t.`level`) as `level`,t.salary_grade,DATE_FORMAT(t.party_date,'%Y-%m-%d %H:%i:%s')as party_date_str,t.party_date,DATE_FORMAT(t.job_date,'%Y-%m-%d %H:%i:%s') as job_date_str,t.job_date,t.unit,t.dept,t.cur_post  from t_emp_base_info t INNER JOIN t_dept_emp_rela d on t.erp_no=d.erp_no INNER JOIN (select * from t_org_dept o where o.path like ?) o2 on o2.id=d.dept_id LEFT JOIN isc_user u on t.erp_no=u.FAX WHERE t.is_valid='0' AND t.del_sign='0' ORDER BY t.create_time desc");
//        StringBuilder sql=new StringBuilder("select t.id,u.`NAME` as user_name,t.erp_no,GET_DIC('Gender',t.sex) as sex,DATE_FORMAT(t.birth_date,'%Y-%m-%d %H:%i:%s') as birth_date_str,t.birth_date,GET_DIC('Nation',t.nation) as nation,t.birth_place,t.born_place,GET_DIC('PoliticalLandscapeText',t.politi_status) as politi_status,t.id_card_no,t.mobile_phone,GET_DIC('StaffLevel',t.`level`) as `level`,t.salary_grade,DATE_FORMAT(t.party_date,'%Y-%m-%d %H:%i:%s')as party_date_str,t.party_date,DATE_FORMAT(t.job_date,'%Y-%m-%d %H:%i:%s') as job_date_str,t.job_date,t.unit,t.dept,t.cur_post ");
//        StringBuilder sql=new StringBuilder("SELECT t.id,u.`NAME` as user_name,t.erp_no,GET_DIC('Gender',t.sex) as sex,t.birth_date,GET_DIC('Nation',t.nation) as nation,t.birth_place,t.born_place,GET_DIC('PoliticalLandscapeText',t.politi_status) as politi_status,t.id_card_no,t.mobile_phone,GET_DIC('StaffLevel',t.`level`) as `level`,t.salary_grade,t.party_date,t.job_date,t.unit,t.dept,t.cur_post ");
//        StringBuilder sql=new StringBuilder("SELECT t.id,u.`NAME` as user_name,t.erp_no,t.sex,t.birth_date,t.nation,t.birth_place,t.born_place,t.politi_status,t.id_card_no,t.mobile_phone,t.`level`,t.salary_grade,t.party_date,t.job_date,t.unit,t.dept,t.cur_post ");
        StringBuilder sql=new StringBuilder("SELECT t.id,u.`NAME` as user_name,t.erp_no,t.sex,t.birth_date,t.nation,t.birth_place,t.born_place,t.politi_status,t.id_card_no,t.mobile_phone,t.`level`,t.salary_grade,t.party_date,t.job_date,t.unit,t.dept,t.cur_post ");
        sql.append("FROM isc_user u LEFT JOIN t_dept_emp_rela TDER ON u.fax = TDER.erp_no LEFT JOIN t_org_dept TOD ON TOD.id = TDER.dept_id LEFT JOIN t_emp_base_info t ON t.erp_no = u.fax ");
        sql.append(" WHERE 1=1 AND TOD.path LIKE ? and t.del_sign = '0' and t.is_valid='0' ");
        sql.append(" ORDER BY t.create_time desc");
        // g.erp_no='11238555'
        params[0]="%"+deptId+"%";
        return this.findAllBySQL(EmpBaseInfoVo.class,sql.toString(),null,false,params);
    }

    @Override
    public List<UserVo> findAllByBaseOrgId(String orgId) {
        Object[] params=new Object[1];
        StringBuilder sql=new StringBuilder("select e.id,e.id as user_id,e.name as user_name,e.erp_no,r.dept_id from t_emp_base_info e ");
        sql.append(" LEFT JOIN t_dept_emp_rela r on e.erp_no = r.erp_no ");
        sql.append("where r.org_id = ?");
        params[0]=orgId;
        return this.findAllBySQL(UserVo.class,sql.toString(),null,false,params);
    }

    @Override
    public List<UserDetailVo> getEmpBaseInfoByOrgId(String userName) {
        List<String> arryList = new ArrayList<String>();
        StringBuilder sql=new StringBuilder("select e.id,e.name as user_name,e.erp_no,r.dept_id,d.name as dept_names from t_emp_base_info e ");
        sql.append(" LEFT JOIN t_dept_emp_rela r on e.erp_no = r.erp_no ");
        sql.append(" LEFT JOIN t_org_dept d on r.dept_id = d.id  ");
        sql.append("where r.org_id = '10342674' ");
            if(!StringUtil.isNullOrEmpty(userName)){
            sql.append(" and e.name like concat('%',?,'%') ");
            arryList.add(userName);
        }
        return this.findAllBySQL(UserDetailVo.class,sql.toString(),null,false,arryList.toArray());
    }
}
